<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <th:block th:include="include :: header('指定会员加油权益列表')"/>
    <th:block th:include="include :: bootstrap-editable-css"/>
</head>
<body class="gray-bg">
<div class="container-div">
    <div class="row">
        <div class="col-sm-12 search-collapse">
            <p class="select-title">会员ID：[<span th:text="${memberId}"></span>] 手机号：[<span th:text="${memberPhone}"></span>]</p>
            <form id="custom-benefit-form">
                <input type="hidden" id="systemModule" name="systemModule" value="1"/>
                <input type="hidden" id="memberId" name="memberId" th:value="${memberId}"/>
            </form>
        </div>
        <div class="col-sm-12 select-table table-striped">
            <table id="bootstrap-table"></table>
        </div>
    </div>
</div>
<th:block th:include="include :: footer"/>
<th:block th:include="include :: bootstrap-table-editable-js"/>
<script th:inline="javascript">
    var prefix = ctx + "back/shopMemberVipBenefit";

    $(function () {
        var options = {
            url: prefix + "/searchCustomList",
            updateUrl: prefix + "/edit/{id}",
            modalName: "会员独享权益",
            onEditableSave: onEditableSave,
            columns: [
                {
                    field: 'id',
                    title: '权益ID',
                    visible:false
                },
                {
                    field: 'productId',
                    title: '油品ID',
                    visible:false
                },
                {
                    field: 'productName',
                    title: '油品',
                    formatter: function (value, row, index) {
                        return row.productCategoryName+value;
                    }
                },
                {
                    field: 'benefitDiscountRate',
                    title: '独享折扣比例',
                    sortable: true,
                    editable: {
                        type: 'text',
                        title: '享受折扣比例',
                        emptytext: "【享受折扣比例】不能为空",
                        validate: function (v) {
                            if ($.trim(v) == '') {
                                return '【享受折扣比例】不能为空!';
                            }
                            if (isNaN(v)) {
                                return '【享受折扣比例】必须是数字';
                            }
                            if (!(/(^[1-9]\d*(\.\d{1,2})?$)|(^0(\.\d{1,2})?$)/.test(v))) {
                                return '请输入正确的【享受折扣比例】';
                            }
                        }
                    }
                },
                {
                    field: 'isEnable',
                    title: '是否享有独有折扣',
                    sortable: true,
                    editable: {
                        type: 'select',
                        title: '是否享有独有折扣',
                        source: [{
                            value: 1,
                            text: "是"
                        }, {
                            value: 0,
                            text: "否"
                        }]
                    }
                },
                {
                    field: 'vipTypeName',
                    title: '当前会员等级及折扣(独享折扣不生效时)',
                    formatter: function (value, row, index) {
                        return value+"享"+parseFloat(row.typeBenefitDiscountRate*10).toFixed(2)+"折";
                    }
                },
            ]
        };
        $.table.init(options);
    });
    function onEditableSave(field, row) {
        let updateData = {};
        updateData[field] = row[field];
        updateData.productId = row.productId;
        updateData.memberId = $('#memberId').val();
        updateData.systemModule = $('#systemModule').val();
        $.ajax({
            type: 'post',
            url: ctx + "back/shopMemberVipBenefit/customEdit",
            data: updateData,
            dataType: 'json',
            async: false,
            success: function (result) {
                if (result.code == web_status.SUCCESS) {
                    $.modal.alertSuccess("修改成功");
                } else {
                    $.modal.alertError(result.msg);
                }
                $.table.refresh();
            }
        });
    }
</script>
</body>
</html>